<?php
/**
 * Management of user roles
 * @author Ice <iceinto@mallog.com.cn>
 */
class RoleUserAction extends CommonAction {
	public function _initialize() {
		parent::_initialize ();
		$this->assign('nav', 7);
	}
	
	public function index() {
		$UserModel = D ( 'User' );
		$RoleUserModel = D('RoleUser');
		$userCount = $UserModel->count();
		$Page = $this->getPage($userCount);
		$users = $UserModel->getList(null, $Page->firstRow.','.$Page->listRows);
		foreach($users as $key=>$user){
			$users[$key]['role'] = $RoleUserModel->getUserRloe($user['id']);
		}
		$this->assign('page', $Page->show());
		$this->assign ( 'users', $users );
		$this->display ();
	}

	/**
	 * Role Management
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function role() {
		$uid = intval ( getGet ( 'id' ) );
		$UserModel = D ( 'User' );
		$RoleUserModel = D ( 'RoleUser' );
		$user = $UserModel->getOne ( $uid );
		if ($user && isPost ()) {
			$this->autoCheckToken ();
			$rid = intval ( getPost ( 'role' ) );
			$RoleUserModel->setUserRloe($uid, $rid);
		}
		$userRoles = $RoleUserModel->getUserRloe ( $uid );
		$roles = M ('Role')->select();

		$this->assign( 'user', $user );
		$this->assign ( 'userRoles', $userRoles );
		$this->assign ( 'roles', $roles );

		$this->display ();
	}

	/**
	 * Mall the role of management
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function mall(){
		$uid = intval(getGet('uid'));
		$MallModel = D('Mall');
		$UserModel = D('User');
		$PerModel = D('Per');
		
		$user = $UserModel->getOne($uid);
		if ($user && isPost ()) {
			$this->autoCheckToken ();
			$mid = intval(getPost('mall'));
			$PerModel->setUserMall($uid, $mid);
		}
		$malls = $MallModel->getList();
		$userMalls = $PerModel->getUserMall($uid);
		
		$this->assign('user', $user);
		$this->assign('malls', $malls);
		$this->assign('userMalls', $userMalls);
		$this->display();
	}
}

?>